using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using AvansRS.Domain;
using AvansRS.Domain.DataInterface;

namespace AvansRS.Web.Controllers
{
    public class EventDateController : Controller
    {
        //
        // GET: /EventDate/
        IRepository<Event> mEventRepository;
        IRepository<EventDate> mEventDateRepository;
        IRepository<Rooster> mRoosterRepository;


        public EventDateController(IRepository<Event> eventRepository, IRepository<EventDate> eventDateRepository, IRepository<Rooster> roosterRepository)
        {
            mEventRepository = eventRepository;
            mEventDateRepository = eventDateRepository;
            mRoosterRepository = roosterRepository;
        }


        public ActionResult Index()
        {
            IList<EventDate> events = mEventDateRepository.GetAll();
            return View(events);
          
        }

        //
        // GET: /EventDate/Details/5

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult EventDateDetails(int id)
        {
            EventDate eventDate = mEventDateRepository.GetByID(id);
            return View(eventDate);
        }

        //
        // GET: /EventDate/Create

      

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EventDateCreate([Bind(Exclude = "ID")] EventDate ev, int eventID, int roosterID)
        {
            Event @event = mEventRepository.GetByID(eventID);
            Rooster @rooster = mRoosterRepository.GetByID(roosterID);
            if (!ModelState.IsValid)
                return View(@event);

          
           // mEventRepository.Add(bla);
            ev.Event = @event;

            ev.Rooster = @rooster;

            mEventDateRepository.Add(ev);

            TempData["Message"] = "EventDate aan event '" + @event.Name + "' toegevoegd.";
            return RedirectToAction("Index", "Event");
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult EventDateCreate(int id)
        {
            SelectList roosterList = new SelectList(mRoosterRepository.GetAll(), "ID", "Name");
            ViewData["roosterList"] = roosterList;
            Event ev = mEventRepository.GetByID(id);

            return View(ev);
        }

        //
        // POST: /EventDate/Create

        [AcceptVerbs(HttpVerbs.Post)]
       

        //
        // GET: /EventDate/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /EventDate/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult EventDateDelete(int id)
        {
            EventDate ev = mEventDateRepository.GetByID(id);
            return View(ev);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EventDateDelete(EventDate ev)
        {
            mEventDateRepository.Delete(ev);
            //mLocationRepository.Save();

            TempData["Message"] = "EventDate verwijderd.";
            return RedirectToAction("Index", "EventDate");
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult EventDateEdit(int id)
        {
            EventDate ev = mEventDateRepository.GetByID(id);
            SelectList eventDateList = new SelectList(mRoosterRepository.GetAll(), "ID", "Name", ev.Rooster.ID);
            ViewData["roosterList"] = eventDateList;

            return View(ev);
        }

        //
        // POST: /Event/EventEdit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EventDateEdit(EventDate ev, int roosterID)
        {
            if (!ModelState.IsValid)
                return View(mEventDateRepository.GetByID(ev.ID));

            Event evRep = mEventRepository.GetByID(ev.ID);
            Rooster rooster = mRoosterRepository.GetByID(roosterID);
            ev.Rooster = rooster;
            mEventDateRepository.Save(ev);


            TempData["Message"] = "Event met naam '" + evRep.Name + "'.";
            return RedirectToAction("Index", "Event");
        }
    }
}
